package com.cn.kehong.service;


import com.cn.kehong.api.IDeviceService;
import com.cn.kehong.dao.SysDeviceMapper;
import com.cn.kehong.domain.SysDevice;
import com.cn.kehong.domain.SysDeviceExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created On : 2023/12/11.
 * <p>
 * Author : Huiyan
 * <p>
 * Description: DeviceServiceImpl
 */
@Service
public class DeviceServiceImpl implements IDeviceService {

    @Autowired
    private SysDeviceMapper sysDeviceMapper;
    @Override
    public List<SysDevice> getDeviceSelect(QueryForm queryForm) throws Exception {
        List<SysDevice> list = sysDeviceMapper.selectDeviceList(queryForm);
        //厂房车间拼接
        for (SysDevice sysDevice:list){
            String str=sysDevice.getFactory()+"-"+sysDevice.getWorkshop();
            sysDevice.setWorkshopStr(str);
        }
        return list;
    }

    @Override
    public String deleteDevice(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if (CollectionsUtil.isEmpty(ids)){
            return "请选中要删除的设备！";
        }
        //TODO 设备表可能关联其他表，判断是否可以删除

        SysDevice device = new SysDevice();
        device.setStatus(Constants.DEL_STATUS);
        SysDeviceExample example = new SysDeviceExample();
        example.createCriteria().andIdIn(ids);
        int count = sysDeviceMapper.updateByExampleSelective(device,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String addDevice(List<SysDevice> sysDevices) throws Exception {


        List<String> equipmentNos = Lists.newArrayList();
        for (SysDevice sysDevice:sysDevices){

            String result = this.deviceLegal(sysDevice);

            //判断修改的物料数据是否满足字段的必填条件
            if (!"".equals(result)){
                return result;
            }

            String deviceId = CommonUtil.generateRandomNum("device_");
            sysDevice.setId(deviceId);
            sysDevice.setCreateTime(new Date());
            equipmentNos.add(sysDevice.getEquipmentNo());
        }

        SysDeviceExample example = new SysDeviceExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andEquipmentNoIn(equipmentNos);
        int existsCount = sysDeviceMapper.countByExample(example);
        if (existsCount > 0){
            return "设备已存在！";
        }

        int count = sysDeviceMapper.batchInsertDevices(sysDevices);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateDevice(SysDevice sysDevice) throws Exception {
        String id = sysDevice.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.deviceLegal(sysDevice);

        //判断修改的数据是否满足字段的必填条件
        if (StringUtils.isNotEmpty(result)){
            return result;
        }

        if (this.countEqnoCode(sysDevice.getEquipmentNo(),id) > 0){
            return "设备号已存在";
        }

        SysDeviceExample example = new SysDeviceExample();
        example.createCriteria().andIdEqualTo(id).andStatusNotEqualTo(Constants.DEL_STATUS);

        int count = sysDeviceMapper.updateByExampleSelective(sysDevice,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public List<SysDevice> getAllDeviceSelect() {

        List<SysDevice> list = sysDeviceMapper.getAllDeviceSelect();
        for(SysDevice sysDevice:list){
            String str=sysDevice.getFactory()+"-"+sysDevice.getWorkshop();
            sysDevice.setWorkshopStr(str);
        }
        return list;
    }

    @Override
    public List<SysDevice> getLineByWorkshop(String workshop) {
        //workshop是15-2的格式需要拆分
        String[] parts = workshop.split("-");
        //厂房
        String factory = parts[0];
        //车间
        String shop = parts[1];
        SysDeviceExample example = new SysDeviceExample();
        example.createCriteria().andFactoryEqualTo(factory).andWorkshopEqualTo(shop).andStatusEqualTo("0");
        List<SysDevice> sysDevices = sysDeviceMapper.selectByExample(example);
        for (SysDevice sysDevice:sysDevices){
            String str=sysDevice.getProductionLine()+"-"+sysDevice.getEquipmentName();
            sysDevice.setLineStr(str);
        }
        return sysDevices;
    }

    private int countEqnoCode(String code,String id) {
        SysDeviceExample example = new SysDeviceExample();
        SysDeviceExample.Criteria criteria = example.createCriteria();

        criteria.andStatusNotEqualTo(Constants.DEL_STATUS).andEquipmentNoEqualTo(code);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }

        int count = sysDeviceMapper.countByExample(example);
        return count;
    }
    private String deviceLegal(SysDevice sysDevice) {

        String result = "";

        if (StringUtils.isBlank(sysDevice.getWorkshop())){
            result = "请选择车间";
            return result;
        }

        if (StringUtils.isBlank(sysDevice.getFactory())){
            result = "请输入厂房";
            return result;
        }


        if (StringUtils.isBlank(sysDevice.getProductionLine())){
            result = "请选择产线";
            return result;
        }

        if (StringUtils.isBlank(sysDevice.getCategory())){
            result = "请选择品类";
            return result;
        }
        if (StringUtils.isBlank(sysDevice.getEquipmentNo())){
            result = "请选择设备号";
            return result;
        }
        if (StringUtils.isBlank(sysDevice.getEquipmentName())){
            result = "请选择设备名称";
            return result;
        }
        return result;
    }
}


